//
//  InfosViewController.m
//  xKey Interface
//
//  Created by Guillaume SOURDRILLE on 16/04/13.
//  Copyright (c) 2013 Edenyorke. All rights reserved.
//

#import "InfosViewController.h"
#import "InfosXmlParser.h"
#import "Infos.h"
#import "InfosCell.h"


@implementation InfosViewController

@synthesize urlConnection;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self initConnection];
       
    [self initData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle: NSLocalizedString(@"button.back", @"") style:UIBarButtonItemStylePlain target:self action:@selector(closeView)];
    self.navigationItem.leftBarButtonItem = backButton;
    
    UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithTitle: NSLocalizedString(@"button.reload", @"") style:UIBarButtonItemStylePlain target:self action:@selector(initData)];
    self.navigationItem.rightBarButtonItem = refreshButton;
    
    
}

- (void) closeView {
    [self dismissModalViewControllerAnimated:YES];
}

- (void)initData {
    NSURL *url = [urlConnection getURLForXml];
    InfosXmlParser *infos = [[InfosXmlParser alloc]init];
    donnees = [infos parseXmlFromUrl:url];
    [self.tableView reloadData];
}



- (void)initConnection {
    
    urlConnection = [[URLConnection alloc] initWithIp];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return donnees.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        
   
    static NSString *MyIdentifier = @"MyIdentifier";
     
    InfosCell *cell = nil;
    
    cell = (InfosCell *) [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    
    if(!cell)
    {
        
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"InfosCell" owner:nil options:nil];
        
        for(id currentObject in topLevelObjects)
        {
            if([currentObject isKindOfClass:[InfosCell class]])
            {
                cell = (InfosCell *)currentObject;
                break;
            }
        }
    }

    
    int index = [indexPath indexAtPosition: [indexPath length] - 1];
    
    Infos* line = [donnees objectAtIndex:index] ;
    
    cell.keyLabel.text =  line.key;
    cell.valueLabel.text =  line.value;
    
        
    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

@end
